package com.ruoyi.basice_info.service.impl;

import java.util.Arrays;
import java.util.List;

import com.ruoyi.basice_info.domain.TsExamDetailsVo;
import com.ruoyi.basice_info.mapper.TsExamDetailsVoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.basice_info.mapper.TbExamRequirementsMapper;
import com.ruoyi.basice_info.domain.TbExamRequirements;
import com.ruoyi.basice_info.service.ITbExamRequirementsService;

/**
 * 考试要求目标设置Service业务层处理
 *
 * @author hongyi
 * @date 2023-12-08
 */
@Service
public class TbExamRequirementsServiceImpl implements ITbExamRequirementsService
{
    @Autowired
    private TbExamRequirementsMapper tbExamRequirementsMapper;



    @Autowired
    private TsExamDetailsVoMapper tsExamDetailsVoMapper;
    /**
     * 查询考试要求目标设置
     *
     * @param examRequirementsId 考试要求目标设置主键
     * @return 考试要求目标设置
     */
    @Override
    public TbExamRequirements selectTbExamRequirementsByExamRequirementsId(Long examRequirementsId)
    {
        return tbExamRequirementsMapper.selectTbExamRequirementsByExamRequirementsId(examRequirementsId);
    }

    @Override
    public TbExamRequirements getTbExamRequirementsByExamRequirementsCode(String code) {
        return tbExamRequirementsMapper.getTbExamRequirementsByExamRequirementsCode(code);
    }

    @Override
    public TbExamRequirements getTbExamRequirementsByExamRequirementsTarget(String target) {
        return tbExamRequirementsMapper.getTbExamRequirementsByExamRequirementsTarget(target);
    }

    /**
     * 查询考试要求目标设置列表
     *
     * @param tbExamRequirements 考试要求目标设置
     * @return 考试要求目标设置
     */
    @Override
    public List<TbExamRequirements> selectTbExamRequirementsList(TbExamRequirements tbExamRequirements)
    {
        return tbExamRequirementsMapper.selectTbExamRequirementsList(tbExamRequirements);
    }

    /**
     * 新增考试要求目标设置
     *
     * @param tbExamRequirements 考试要求目标设置
     * @return 结果
     */
    @Override
    public int insertTbExamRequirements(TbExamRequirements tbExamRequirements)
    {
        return tbExamRequirementsMapper.insertTbExamRequirements(tbExamRequirements);
    }

    /**
     * 修改考试要求目标设置
     *
     * @param tbExamRequirements 考试要求目标设置
     * @return 结果
     */
    @Override
    public int updateTbExamRequirements(TbExamRequirements tbExamRequirements)
    {
        return tbExamRequirementsMapper.updateTbExamRequirements(tbExamRequirements);
    }

    /**
     * 批量删除考试要求目标设置
     *
     * @param examRequirementsIds 需要删除的考试要求目标设置主键
     * @return 结果
     */
    @Override
    public int deleteTbExamRequirementsByExamRequirementsIds(Long[] examRequirementsIds)
    {

        int num = -1;
        int nums = 0;
        List<Long> longList = Arrays.asList(examRequirementsIds);
        for (Long item: longList) {
            TsExamDetailsVo detailsVo = new TsExamDetailsVo();
//            detailsVo.setExamDetailsId(item);
            detailsVo.setExamRequirementsId(item);
            // 去子表中查询
            // 删除一条的
            List<TsExamDetailsVo> voList = tsExamDetailsVoMapper.selectTsExamDetailsList(detailsVo);
            if (voList.size()<=0) {
                num = tbExamRequirementsMapper.deleteTbExamRequirementsByExamRequirementsId(item);
            } else  {
                nums ++;
            }
        }
        // 删除多条的；
        if (longList.size() == 1) {
            if(nums > 0) {
                throw new IllegalArgumentException("该数据考试详情中使用，不能删除");
            } else {
                if(nums > 0) {
                    throw new IllegalArgumentException(nums + " 条数据删除失败，该数据考试详情中使用，不能删除");
                }
            }
        }

//        return tbKnowledgePointsMapper.deleteTbKnowledgePointsByKnowledgePointsIds(knowledgePointsIds);

        return num;

    }

    /**
     * 删除考试要求目标设置信息
     *
     * @param examRequirementsId 考试要求目标设置主键
     * @return 结果
     */
    @Override
    public int deleteTbExamRequirementsByExamRequirementsId(Long examRequirementsId)
    {
        return tbExamRequirementsMapper.deleteTbExamRequirementsByExamRequirementsId(examRequirementsId);
    }
}
